有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

如何在java中设置y/n循环?

我有一个程序,它是一个回文检查器,它接受一个输入,将其反转,并检查原始输入是否等于反转。我正试图在程序中得到一个y/n循环,以检查用户是否想要输入另一个回文。这是我的代码:

  public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        String controller = "y";
    do {

        System.out.println("Enter a palindrome!");
        String input = scan.nextLine();
        String original = input;
        input = input.replaceAll("\\s", "");
        int len = input.length();
        char[] charArray = new char[len];
        char[] charArray2 = new char[len];

        for(int i = 0; i < len; i++){
            charArray2[i] = input.charAt(i);
        }
        for(int j = 0; j < len; j++){
            charArray[j] = charArray2[len-1-j];
        }
        String palindrome = new String(charArray);
        if(palindrome.equals(input)){
            System.out.println(palindrome);
            System.out.println(original + " is a palindrome!\nWould you like to test another palindrome? (y/n)");
        }else{
            System.out.println(palindrome);
            System.out.println(original + " is not a palindrome!\nWould you like to test another palindrome? (y/n)");

        }
        scan.next(controller);

    }while(controller.equalsIgnoreCase("y"));

    }

这是我得到的输出:

Enter a palindrome!
was it a cat i saw
wasitacatisaw
was it a cat i saw is a palindrome!
Would you like to test another palindrome? (y/n)
y
Enter a palindrome!

 is a palindrome!
Would you like to test another palindrome? (y/n)
y
Enter a palindrome!

 is a palindrome!
Would you like to test another palindrome? (y/n)
y
Enter a palindrome!

 is a palindrome!
Would you like to test another palindrome? (y/n)
y
Enter a palindrome!

 is a palindrome!
Would you like to test another palindrome? (y/n)
y
Enter a palindrome!

 is a palindrome!
Would you like to test another palindrome? (y/n)
y

我不知道如何让它等待另一个条目并测试该条目。任何帮助都会很好


共 (1) 个答案

  1. # 1 楼答案

    好的,我重新修改了你的代码,你离我不远了。 Here is a pastebin link使用我的代码

    public static void main(String[] args) {
        String checker = "y"; //set checker to ensure the first cycle
    
        while(checker.equalsIgnoreCase("y")) { // check for correct phrase
            System.out.println("Enter a string to check!"); // inital message
            Scanner reader = new Scanner(System.in);        // read first line
            String input = reader.nextLine();
            if (isPalindrome(input)) {                      // check input for palindrome
                System.out.println(input + " is a Palindrome!");
            } else {
                System.out.println(input + " is not a Palindrome!");
            }
            System.out.println("Do you want to enter another string? y/n");
            checker = reader.nextLine();                    // set checker to response for next cycle
        }
        System.out.println("Ok bye :)");                    // death message
    }
    
    
    private static boolean isPalindrome(String s){
        String reverse = "";                            // create string for later comparison
    
        for (int i = 0; i < s.length(); i++) {
            reverse+=s.trim().charAt(s.length()-i-1);   // reverse the string
        }
        return reverse.equalsIgnoreCase(s);             // return boolean ignoring case
    }
    

    我真的没有改变那么多:

    • 我检查函数中的回文以获得更好的可读性
    • 使用字符串而不是字符[]。这样处理用户输入和一般文本通常更好

    编辑: 我的输出:

    Enter a string to check!
    hey
    hey is not a Palindrome!
    Do you want to enter another string? y/n
    y
    Enter a string to check!
    brb
    brb is a Palindrome!
    Do you want to enter another string? y/n
    n
    Ok bye :)
    
    Process finished with exit code 0